package com.ohos.carbon.slice.animations;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.utils.FocusUtil;
import com.ohos.carbon.utils.RandomImageUtil;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.*;
import ohos.agp.components.element.ElementScatter;
import ohos.multimodalinput.event.TouchEvent;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class PathAnimationSlice extends AbilitySlice {
    private Image back_button, image1, image2, imageView;
    private Text textTitle;
    private RandomImageUtil randomImageUtil;
    private DirectionalLayout dirLayout, dirText;
    private DependentLayout dirCard, dep;
    private boolean isOpen = false;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_path_animation);
        //创建RandomImageUtil对象
        randomImageUtil = new RandomImageUtil(getContext());
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Path animation")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Path animation")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Path animation", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Path animation", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Path animation", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        //titlebar
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        image1 = (Image) findComponentById(ResourceTable.Id_image1);
        image2 = (Image) findComponentById(ResourceTable.Id_image2);
        textTitle = (Text) findComponentById(ResourceTable.Id_title_text);
        textTitle.setText("Path animation");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        dirCard = (DependentLayout) findComponentById(ResourceTable.Id_card);
        dirLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_dir_layout);
        dirText = (DirectionalLayout) findComponentById(ResourceTable.Id_dir_text);
        dep = (DependentLayout) findComponentById(ResourceTable.Id_dep);
        //随机展示及图片
        imageView = (Image) findComponentById(ResourceTable.Id_image);
        imageView.setPixelMap(randomImageUtil.next2());
        //点击返回上个页面
        back_button.setClickedListener(component -> {
            onBackPressed();
            dirCard.setContentPosition(0, 0);
        });
        imageView.setVisibility(Component.VISIBLE);
        dirText.setVisibility(Component.VISIBLE);
        //卡片功能
        AnimatorValue animator = new AnimatorValue();
        animator.setDuration(5000);
        animator.setCurveType(Animator.CurveType.OVERSHOOT);
        dirLayout.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (isOpen == false) {
                    imageView.setVisibility(Component.VISIBLE);
                    dirText.setVisibility(Component.VISIBLE);
                    isOpen = true;
                } else {
                    imageView.setVisibility(Component.HIDE);
                    dirText.setVisibility(Component.HIDE);
                    dep.setVisibility(Component.VISIBLE);
                    isOpen = false;
                }
                animator.start();
            }
        });
        //整个布局的触摸事件的监听
        dirLayout.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                int action = touchEvent.getAction();
                switch (action) {
                    case TouchEvent.PRIMARY_POINT_DOWN:
                    case TouchEvent.POINT_MOVE:
                        dirCard.setContentPosition(FocusUtil.getWx(touchEvent) - 550, FocusUtil.getFocus(touchEvent) - 600);
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackPressed() {
        super.onBackPressed();
        dirCard.setContentPosition(0, 0);
    }
}
